package cn.hdustea.shopNavigation.ui.view.main

import android.content.Context
import android.content.Intent
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import cn.hdustea.shopNavigation.R
import cn.hdustea.shopNavigation.adapter.StoreItemAdapter
import cn.hdustea.shopNavigation.config.ConstantUrl
import cn.hdustea.shopNavigation.entity.Store
import cn.hdustea.shopNavigation.ui.contract.StoreContract
import cn.hdustea.shopNavigation.ui.presenter.StorePresenter
import com.bumptech.glide.Glide
import com.hdustea.base.base.BaseActivity
import com.hdustea.base.widget.CustomLoadMoreView
import com.hdustea.base.widget.SpacesItemDecoration
import kotlinx.android.synthetic.main.activity_food.*

/**
 * <pre>
 *     author : HuQiang
 *     time   : 2020/09/22
 *     desc   :
 *     version: 1.0
 * </pre>
 */
class StoreActivity : BaseActivity<StoreContract.Presenter>(), StoreContract.View {

    private var storeAdapter: StoreItemAdapter? = null

    private var mCurrentPage: Int = 0

    companion object {

        fun startActivity(context: Context) {
            context.startActivity(Intent(context, StoreActivity::class.java))
        }

    }

    override fun initView() {
        Glide.with(this)
                .load("${ConstantUrl.BASE_URL}/image/banner/special.jpg")
                .into(im_banner)
        val layoutManager = StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL)
        recycler_view.layoutManager = layoutManager
        recycler_view.isNestedScrollingEnabled = true
        recycler_view.addItemDecoration(SpacesItemDecoration(16))

        showLoading()
    }

    override fun initData() {
        mPresenter?.getStoreList(0, false)
    }

    private fun initRecyclerView() {
        storeAdapter!!.animationEnable = true
        storeAdapter!!.loadMoreModule.loadMoreView = CustomLoadMoreView()
        storeAdapter!!.loadMoreModule.setOnLoadMoreListener {
            mPresenter?.getStoreList(mCurrentPage + 1, true)
        }
        storeAdapter!!.loadMoreModule.isAutoLoadMore = true
        //当自动加载开启，同时数据不满一屏时，是否继续执行自动加载更多(默认为true)
        storeAdapter!!.loadMoreModule.isEnableLoadMoreIfNotFullPage = false
    }

    override fun getLayoutRes(): Int = R.layout.activity_store

    override fun getPresenter(): StoreContract.Presenter = StorePresenter(this)

    override fun setStoreList(storeList: MutableList<Store>, isRefresh: Boolean) {
        if (!isRefresh) {
            storeAdapter = StoreItemAdapter(R.layout.item_list_store, this)
            initRecyclerView()
            recycler_view.adapter = storeAdapter
            storeAdapter!!.setList(storeList)
        } else {
            if (storeList.isNotEmpty()) {
                mCurrentPage++
                storeAdapter!!.addData(storeList)
                storeAdapter!!.loadMoreModule.loadMoreComplete()
            } else {
                storeAdapter!!.loadMoreModule.loadMoreEnd()
            }
        }
    }

    override fun onError() {
        storeAdapter!!.loadMoreModule.isEnableLoadMore = true
        storeAdapter!!.loadMoreModule.loadMoreFail()
    }

}